package com.richard.library.context;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.Window;

import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;

import com.richard.library.context.life.ActivityLifecycleImpl;

/**
 * <pre>
 * Description : App Context 统一管理
 * Author : admin-richard
 * Date : 2018/6/20 11:34
 * Changelog:
 * Version            Date            Author              Detail
 * ----------------------------------------------------------------------
 * 1.0         2018/6/20 11:34     admin-richard         new file.
 * </pre>
 */
public final class AppContext {

    private static Application application;
    private static boolean isDebug;

    //显示Dialog时是否隐藏底部导航栏按钮和状态栏
    private static boolean isHideBar;

    private AppContext() {
    }

    /**
     * 初始化
     *
     * @param application application
     * @param isHideBar   显示Dialog时是否隐藏底部导航栏按钮和状态栏
     * @param isDebug     是否debug模式
     */
    public static void init(Application application, boolean isHideBar, boolean isDebug) {
        AppContext.isDebug = isDebug;
        AppContext.isHideBar = isHideBar;

        if (AppContext.application == null) {
            AppContext.application = application;
            ActivityLifecycleImpl.INSTANCE.init(AppContext.application);
            return;
        }

        if (AppContext.application.equals(application)) return;
        ActivityLifecycleImpl.INSTANCE.unInit(AppContext.application);
        AppContext.application = application;
        ActivityLifecycleImpl.INSTANCE.init(AppContext.application);
    }

    /**
     * 获取applicationContext
     */
    public static Context get() {
        if (application == null) {
            Log.e("error", "Uninitialized ApplicationContext");
        }
        return application;
    }

    /**
     * 获取Application
     */
    public static Application getApplication() {
        return application;
    }

    /**
     * 获取显示Dialog时是否隐藏底部导航栏按钮和状态栏
     */
    public static boolean isHideBar() {
        return isHideBar;
    }

    /**
     * 应用是否属于debug模式
     */
    public static boolean isDebug() {
        return isDebug;
    }

    /**
     * 获取应用包名
     */
    public static String getPackageName() {
        return application.getPackageName();
    }

    /**
     * 获取应用resource
     */
    public static Resources getResources() {
        return get().getResources();
    }

    /**
     * 获取string
     */
    public static String getString(@StringRes int id) {
        return getResources().getString(id);
    }

    /**
     * 获取格式化后的string
     *
     * @param formatId   格式化文本资源id
     * @param formatArgs 填充文本
     */
    public static String getString(@StringRes int formatId, Object... formatArgs) {
        return format(getString(formatId), formatArgs);
    }

    /**
     * 格式化字符
     *
     * @param format     格式化
     * @param formatArgs 填充文本
     */
    public static String format(String format, Object... formatArgs) {
        return String.format(format, formatArgs);
    }

    /**
     * 获取颜色值
     */
    public static int getColor(@ColorRes int id) {
        return getResources().getColor(id);
    }

    /**
     * 获取颜色值
     */
    @SuppressWarnings("all")
    public static int getColor(@ColorRes int id, Resources.Theme theme) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return getResources().getColor(id, theme);
        }
        return getResources().getColor(id);
    }

    /**
     * 获取px大小
     */
    public static int getDimensionPixelSize(@DimenRes int id) {
        return getResources().getDimensionPixelSize(id);
    }

    /**
     * 获取大小（单位px）
     */
    public static int getDimension(@DimenRes int id) {
        return (int) getResources().getDimension(id);
    }

    /**
     * 获取Drawable对象
     */
    public static Drawable getDrawable(@DrawableRes int id) {
        return getDrawable(id, null);
    }

    /**
     * 获取Drawable对象
     */
    public static Drawable getDrawable(@DrawableRes int id, Resources.Theme theme) {
        @SuppressLint("UseCompatLoadingForDrawables") Drawable drawable;
        if (theme == null) {
            drawable = getResources().getDrawable(id);
        } else {
            drawable = getResources().getDrawable(id, theme);
        }
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        return drawable;
    }

    /**
     * 当前是否是横屏
     */
    public static boolean isScreenLandscape() {
        return getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
    }

    /**
     * 当前是否是竖屏
     */
    public static boolean isScreenPortrait() {
        return getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    }

    /**
     * 安全的获取window
     */
    public static Window getWindow(View view) {
        Context context = getUIContext(view.getContext());
        if (context != null) {
            return ((Activity) context).getWindow();
        }
        return null;
    }

    /**
     * 安全的获取Activity的context
     */
    public static Context getUIContext(View view) {
        return getUIContext(view.getContext());
    }

    /**
     * 安全的获取Activity的context
     *
     * @param context 上下文
     * @return 提取到的 Activity，否则为 null。
     */
    public static Context getUIContext(Context context) {
        if (context == null) {
            return null;
        }
        if (context instanceof Activity) {
            return context;
        } else if (context instanceof ContextWrapper) {
            Context baseContext = ((ContextWrapper) context).getBaseContext();
            return getUIContext(baseContext);
        }
        return null;
    }

    public static Activity getActivity(Context context) {
        if (getUIContext(context) instanceof Activity activity) {
            return activity;
        }
        return null;
    }
}
